package com.css.apps.base.org.model;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.css.apps.base.dict.service.DictMan;
import com.css.apps.base.post.model.SPost;
import com.css.apps.base.user.model.SUser;
import com.css.core.model.IOrg;
import com.css.core.model.tree.TreeType;
import com.css.db.query.JoinList;
import com.css.db.query.QueryCache;
import com.css.util.HtmlConverter;
import com.css.util.Json.Unjson;

import net.sf.json.JSONObject;

@SuppressWarnings("serial")
public class SOrg implements IOrg, Serializable, TreeType {

	private String className = SOrg.class.getName();
	private String uuid;
	private String name;
	private String parentId;
	private Integer orderNum;
	private String delFlag;
	private String openFlag;
	private String remark;
	private Date issueDate;
	private String issueId;
	private String issueName;
	private Date editDate;
	private String code;
	private String orgType;
	private int orgLevel;
	/**
	 * 定义关联岗位、用户列表
	 */
	private transient JoinList postList = null;
	private transient JoinList userList = null;

	public String getLogInfo() {
		StringBuffer sb = new StringBuffer();
		sb.append(HtmlConverter.appendHtmlNameField("主键", this.getUuid()));
		sb.append(HtmlConverter.appendHtmlNameField("名称", this.getName()));
		sb.append(HtmlConverter.appendHtmlNameField("父节点", this.getParentId()));
		sb.append(HtmlConverter.appendHtmlNameField("排序号", this.getOrderNum()));
		sb.append(HtmlConverter.appendHtmlNameField("删除标记",
				DictMan.getDictType("d_dealflag", this.getDelFlag()).getName()));
		sb.append(HtmlConverter.appendHtmlNameField("开启标记",
				DictMan.getDictType("d_openflag", this.getOpenFlag()).getName()));
		sb.append(HtmlConverter.appendHtmlNameField("备注", this.getRemark()));
		SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		sb.append(HtmlConverter.appendHtmlNameField("创建日期", sdf.format(this.getIssueDate())));
		sb.append(HtmlConverter.appendHtmlNameField("创建人id", this.getIssueId()));
		sb.append(HtmlConverter.appendHtmlNameField("创建人", this.getIssueName()));
		sb.append(HtmlConverter.appendHtmlNameField("修改日期", sdf.format(this.editDate)));
		sb.append(HtmlConverter.appendHtmlNameField("机构类型", this.getOrgType()));
		sb.append(HtmlConverter.appendHtmlNameField("机构层级", this.getOrgLevel()));
		return sb.toString();
	}

	public String getUuid() {
		return uuid;
	}

	public void setUuid(String uuid) {
		this.uuid = uuid;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getParentId() {
		return parentId;
	}

	public void setParentId(String parentId) {
		this.parentId = parentId;
	}

	public Integer getOrderNum() {
		return orderNum;
	}

	public void setOrderNum(Integer orderNum) {
		this.orderNum = orderNum;
	}

	public String getDelFlag() {
		return delFlag;
	}

	public void setDelFlag(String delFlag) {
		this.delFlag = delFlag;
	}

	public String getOpenFlag() {
		return openFlag;
	}

	public void setOpenFlag(String openFlag) {
		this.openFlag = openFlag;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public Date getIssueDate() {
		return issueDate;
	}

	public void setIssueDate(Date issueDate) {
		this.issueDate = issueDate;
	}

	public String getIssueId() {
		return issueId;
	}

	public void setIssueId(String issueId) {
		this.issueId = issueId;
	}

	public String getIssueName() {
		return issueName;
	}

	public void setIssueName(String issueName) {
		this.issueName = issueName;
	}

	public Date getEditDate() {
		return editDate;
	}

	public void setEditDate(Date editDate) {
		this.editDate = editDate;
	}

	public String getClassName() {
		return className;
	}

	public void setClassName(String className) {
		this.className = className;
	}

	@Unjson
	public JoinList getPostList() {
		if (postList == null) {
			QueryCache qc = new QueryCache("select a.postId from SOrgPost a where a.orgId=:orgId").setParameter("orgId", uuid);
			postList = new JoinList(SPost.class, qc);
		}
		return postList;
	}

	@Unjson
	public JoinList getUserList() {
		if (userList == null) {
			QueryCache qc = new QueryCache("select a.userId from SUserOrg a where a.deptId = :deptId").setParameter("deptId", uuid);
			userList = new JoinList(SUser.class, qc);
		}
		return userList;
	}

	/**
	 * 转换为单个json字符串
	 * 
	 * @return
	 */
	public JSONObject toJson() {
		return new JSONObject().element("uuid", uuid).element("name", name).element("parentId", parentId)
				.element("orderNum", orderNum).element("delFlag", delFlag).element("openFlag", openFlag)
				.element("remark", remark).element("issueDate", issueDate).element("issueId", issueId)
				.element("issueName", issueName).element("editDate", editDate).element("orgType", orgType)
				.element("orgLevel", orgLevel);
	}

	public String getOrgId() {
		return this.uuid;
	}

	public String getOrgName() {
		return this.name;
	}

	@Override
	public String getNodeId() {
		return uuid;
	}

	@Override
	public String getNodeName() {
		return name;
	}

	@Override
	public String getNodeParentId() {
		return parentId;
	}

	@Override
	public String getRootSerializeId() {
		return "root";
	}

	@Override
	public Serializable getSerializeId() {
		return uuid;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<String> getTreeList() {
		return new QueryCache("select uuid from SOrg where delFlag = '2' and openFlag = '1' order by orderNum").list();
	}

	@Override
	public List<String> getTreeList(String key) {
		return getTreeList();
	}

	public String getOrgType() {
		return orgType;
	}

	public void setOrgType(String orgType) {
		this.orgType = orgType;
	}

	public int getOrgLevel() {
		return orgLevel;
	}

	public void setOrgLevel(int orgLevel) {
		this.orgLevel = orgLevel;
	}

	@Override
	public String getOrgCode() {
		return code;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

}